package com.zpark.it.controller;

import com.zpark.it.Utils.AliyunOSSUtils;
import com.zpark.it.param.ProductSaveParam;
import com.zpark.it.param.ProductSearchParam;
import com.zpark.it.pojo.Product;
import com.zpark.it.service.ProductService;
import com.zpark.it.utils.R;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Period;
import java.util.UUID;

/**
 * @Title: ProductController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/27 17:14
 */
@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @GetMapping("list")
    public R adminList(ProductSearchParam productSearchParam){
        return productService.search(productSearchParam);
    }

    @PostMapping("upload")
    public R adminUpload(@RequestParam("img")MultipartFile img) throws Exception {
        String filename = img.getOriginalFilename();
        filename= UUID.randomUUID().toString().replace("-","")+
                filename;
        String contentType = img.getContentType();

        byte[] content = img.getBytes();

        int hours=1000;

        String url = aliyunOSSUtils.uploadImage(filename, content, contentType, hours);
        System.out.println("url="+url);
        return  R.ok("图片上传成功，",url);

    }

    @PostMapping("save")
    public R adminSave( ProductSaveParam productSaveParam){
        return productService.save(productSaveParam);

    }

    @PostMapping("update")
    public R adminUpdate( Product product){
        return productService.update(product);
    }

    @PostMapping("remove")
    public R adminRemove( Integer productId){
        return productService.remove(productId);
    }

}
